Here is an excerpt from Sayed Ahmed regarding an evolutionary aspect to answer this question:

As for the case of the human who has a feeling that he is carrying a (tendency to be a) female within him while he is a male or vice versa, we can understand this issue by knowing that sex determination came at a late stage of evolution on planet Earth. Life did not begin with sexual reproduction. The beginning was with asexual reproduction and sexual reproduction came later. It has several orientations; one of them is the animal that is a hermaphrodite, i.e., having both masculine and feminine reproductive organs. Another orientation includes a type of fish, which at some stages in its life is male with fully developed reproductive organs. He fertilizes, and then in a different stage transforms into a female with fully developed reproductive organs and she lays eggs. Another orientation is our case, where there is a male and a female who are separate and whose sexes are determined for their entire lives. In this case, the fetus has the potential to be a male or a female. What determines the growth of certain genitals and not others are the sex hormones. This means that within each body there is a male and a female, or let’s say the potential to be male or female. What happens is that one of them dominates when implementing the genetic blueprint specific for this body, so the fetus becomes male or female. This issue is clear in some conditions or chromosome abnormalities, as is the case in androgen insensitivity syndrome, where sometimes the case is a female with internal male organs. I mentioned this in The Atheism Delusion, and here is the text to clarify this issue for you:

“There is a genetic mutation that affects the androgen receptors and it is called the masculinization hormone [androgen] syndrome in the fetus (46XY). This mutation inhibits the effect of the male sex gene; thus, a feminine reproductive system is formed. The case of one afflicted with this syndrome depends on the type of mutation and the degree of its effect on androgen receptors. So he could have incomplete male and female organs and one of them might suffice. But in general there are feminine characteristics.So if the fetus carrying male genes, meaning (46XY), suffers from a complete androgen insensitivity syndrome, externally he would be completely female. In fact, it is usually difficult to diagnose the case early without precise testing. So the fetus has apparent feminine organs, so he is usually a female, but she is without a uterus or ovaries, meaning she is a female with an incomplete female reproductive system, and she also has an internal male reproductive system that is concealed within the pelvis.”

According to the above, we can understand that each male carries a dead female within him, and each female carries a dead male within her. What homosexuals do is psychologically revive this buried physical interior that became obsolete during fetal development, although actually and practically, there is no significant, real and actual trace of it in the body. And a male might have to play a female role, and the female might have to play a male role. And the truth is, not only does the human being practice this, but other animals practice this as well by 10 percent, perhaps slightly more or less, and the reason for that is what I clarified above. With this statement, it is made clear that homosexuality is not natural just because other animals practice it. It is an issue of abnormality and departure from the nature that dominates the body of the living being. What is natural is to place laws that prevent and limit this abnormality, and not the opposite. However, and unfortunately, the fact that homosexuality is a common characteristic in the animal kingdom was used as an excuse to persuade some politicians in Britain and France, most of whom are ignorant about evolutionary biology, to pass gay marriage laws.