After the magi leave Herod the star appears and leads them to where Jesus is, so why didn't the star appear to them and lead them to Jesus before they reached Jerusalem?
Had it done so, this would have stopped Herod from trying to kill Jesus and murdering innocent children along the way.
I'm a Christian of the Reformed tradition, which emphasise the sovereignty of God in and over all things. So, yes, God had purpose in just how things unfolded. We aren't told all about His purposes, though we certainly are told a great deal about His character. We are to believe and hold fast that God is both in control and completely righteous, including in this troubling account.
We are told that all of Jerusalem was "troubled" with the coming of the Wise Men to the city, their message, and perhaps King Herod's likely response (for they knew he was obsessed over being usurped). And, yes, we are told more than once in the passage that divine prophecy was being fulfilled, as well as messengers of God (angels speaking through dreams - not insignificant events) being intimately involved in some aspects of what transpires (maybe even including the "star").
The best we can say is that God's righteous ordinances in the fulfillment of all His purposes is accomplished, without any just charge of evil besmirching His holy character. Though again, yes, we aren't given the complete insight into how all of this acts to glorify Him and His divine character, purposes, and will.
I would agree that since before time, the "slaughter of the innocents" was both ordained and accomplished by God through His holy use of the evil of men, yet without any just assignation to sin or any hint of unholiness in God's actions or divine purposes.
God can always stop any evil we do. And yet OUR commission of evil accomplishes His divine purpose, with all just judgement upon US, and no castigation clouding the holy character of God; but rather glorifying His mercy and love to us, even in the deaths of infants. "Are not two sparrows sold for a penny? Yet not one of them will fall to the ground outside your Father’s care. And even the very hairs of your head are all numbered." (Matt 10: 29-30)