Many Theological reasons (including famous Prophecy in Isaiah).
One simple reason is to provide a way around the blood curse on Jeconiah.
Thus saith the Lord, write Ye this man Childless, a man that shall not prosper in his days: for no man of his seed shall prosper, siting upon the throne of David, and ruling any more in Judah. - Jeremiah 22:30
Mary’s entitlement to Inheritance (having married Jospeh from the same tribe). Her father Heli would have adopted Joseph as his Son-in-Law.
As per Jewish Law: An exception is provided for the daughters of Zelophehad for inheritance if no sons were born and if she married within Tribe. (Numbers 26:33, 27:1-11, 36:2-12, Joshua 17:3-6, 1 Chronicles 7:15).
By tradition, the father would legally adopt his son-in-law in such cases. (Ezra 2:61, Nehemiah 7:63, 32:41 and also see 1 Chronicles 2:21-23, 34-35, Numbers 27:3-8)
In Luke 3:23 we see - And Jesus himself began to be about thirty years of age, being (as was supposed) the son of Joseph, which was the Son of Heli, (Greek word used here is “Nomizo” Strong’s G3543 as legalised or what we call today “Son-in-Law”).