This particular excerpt below focuses on what Matthew thought about the historicity of the flight into Egypt.
"What did Matthew think? Did he believe this event actually occurred? Or, was he consciously transmitting a “theological story,” knowing the event never occurred? This scenario is quite unlikely. He probably did not even think about any of this. Christians early on probably did not think about it either. Anyways, a straightforward reading of these chapters and the Gospel as a whole does not make us think so. In fact, up until the late eighteenth or early nineteenth century, all of Christendom basically believed that the massacre of the innocents actually occurred.
It is not until the nineteenth century that we hear the first voices against the historicity of this portion of the narrative. It is not until the twentieth and twenty-first centuries that we hear the hypothesis that this is a “theological history,” transmitting not the events as they happened, but a religious message. Nevertheless, it is normal to think that Matthew believed everything occurred as he communicated it to his audience.
For us today, this question regarding the historicity of what we read in the Gospels is very important because it depends on the basic ideological sustenance of life. Matthew composed the story of the magi in his attempt to provide a more complete biography of Jesus. Most likely, he used previously existing legends that had been developed in his community. Matthew takes these legends, rewrites and rearranges them, and then incorporates his own new material, especially from the Old Testament. He does all of this to support one central idea, specifically that Jesus is the Messiah and had a miraculous childhood, like other heroes of old, full of wonders, which confirm that he is the true Son of God."