"Then, Moses commanded them, saying, At the end of [every] seven years, at an appointed time, in the Festival of Succoth, [after] the year of release,
When all Israel comes to appear before the Lord, your God, in the place He will choose, you shall read this Torah before all Israel, in their ears.
Assemble the people: the men, the women, and the children, and your stranger in your cities, in order that they hear, and in order that they learn and fear the Lord, your God, and they will observe to do all the words of this Torah." ...
(Deut. 30: 10-12) (Translation - Chabad.org)
Rashi: the children: Why did they come? So that a reward would be given to those who bring them along. — [Chag. 3a]
Here, the Hebrew word for specifically very small children is used ("Taf")
The Gemara in Talmud tractate Chagigah page 3a wonders why the Torah would command us to bring these small children to hear the public Torah study during the "Mitzvah of Hakhel" ?
"They said to him that Rabbi Elazar ben Azarya interpreted the following verse: “Assemble the people, the men and the women and the little ones” (Deuteronomy 31:12). This verse is puzzling: If men come to learn, and women, who might not understand, come at least to hear, why do the little ones come? They come in order for G-d to give a reward to those who bring them, i.e., G-d credits those who bring their children to the assembly. Rabbi Yehoshua said to them: This good pearl of wisdom was in your hands, and you tried to conceal it from me?"
(Talmud Tractate Chagigah 3a - Translation- Sefaria)
So we see from here that when dealing with people who will not be able to absorb anything from the Torah lecture (very small children), the Torah is still very excited when they participate and a reward is bestowed upon their parents and family for bringing them along.
Sometimes those who are incapable of themselves observing Jewish law are in fact created so that others may involve them in the Torah environment and thereby these "children" help us all do more mitzvos and gain reward.
(as an aside, if you will argue that those children would at least eventually grow up as normal adults who would in fact be self-observant etc., and their position as infants is only temporary; still that is no objection, as G-d Himself knows that a certain minority of these "Taf" are created and destined to pass away at that young age and were never created to "grow up" passed the age of "Taf". Still, the Torah commands us to bring these created children to the Torah reading ; who will never absorb or understand it in the simple sense.)
The second thing we learn from "the Mitzvah of Hakhel" is the very fact that G-d does not only value observance made by the stand alone free choice of the worshipper.
The fact that these children are "involved" in the Mitzvah and can thereby bring reward to their family, is worth it all by itself. They do not need to have been able to choose an observant life to be leading an observant life. (For instance, all observant families I know of in the Orthodox Jewish community, will always spend the extra funds and feed their mentally challenged family members exclusively Kosher meals.)
The Ben Yehoyada (a Talmudic commentary) explains on this passage of Chag. 3a, that we learn from here on a mystical level, that people who could not possibly understand the Torah learning still can absorb it in their soul, and will receive reward for it and even grow to understand it in the World to Come. In fact, he proves this from the hint given that R' Yehoshua ben Chananiah exclaimed: "This good pearl of wisdom was in your hands, and you tried to conceal it from me?"
R' Yehoshua was praised by his Rebbe R' Yochanan ben Zakkai with the term "happy are those who gave birth to you". (See Pirkei Avos Ch 2. ) This is considered by some commentators, as a reference to the known practice of R' Yehoshua's mother who used to park his stroller when he was a baby, inside the Torah study hall.
Of course there are some stories brought in the mystical writings of the Arizal that show how souls can be reincarnated with defects in this world in order to repair sins of past lives. (See Shaar HaGilgulim of R' Chaim Vital, student of the Arizal, at length.)
On this note, one might wish to read the modern day book "Galia: Messages from Heaven" about the revelations from children of autism (or brain damage) about their past lives.
However, this is not to say that all insane people sinned in a past life.
"Hakhel" teaches that they were created so we should love them, involve them in Torah life, and gain reward.
The Talmud in Tractate Bava Basra 12b states:
"Rabbi Yochanan said: "After the destruction of the Holy Temple the power of prophecy was taken from the prophets and given to the mentally ill and to children."
My personal take on this teaching is that since the world without the Temple is a very difficult battleground for succeeding in spirituality, prophecy was given to these, so that man can have an excuse for why he didn't obey the prophecy.... that guy was nuts, I didn't know I should take it that seriously!
Also, I remember learning a Medrash in Yeshiva that said that once King David asked G-d what purpose insanity could possibly have in the world?
G-d promised him that one day he himself would find use for it. Eventually King David was forced to flee before King Saul and joined the Phillistine King's army for a time. The Phillistines eventually suspected him of still being a loyal Jew. So, before they could imprison him, King David faked insanity and they let him go.
The Medrash says that the Phillistine King Achish of Gath had an insane daughter at that time and King David and that girl would howl at the moon all night while King David also wrote messages on the palace walls about how Achish and the Queen owed him money.
The royals couldn't take it anymore, but were very convinced that David was no threat.
(See I Samuel 21: 11-16, and also Medrash Tehillim 34)
Finally, I remember an anecdote with no real source, except that I have heard it told more than once, in different Yeshivahs where I learned :
The Rambam was also a famous doctor. Once, some sages asked him how many physical illneses are there in the world? The rambam calculated an dgave them a certain number, such and such.
Then they asked the Rambam how many forms of mental illness were in the world?
The Rambam quickly retorted: "What is the number of the population of the Earth right now?"
So apparently, the OP might not be so bothered if we realize that somewhere along the line, in this world, we are all a little crazy. That shouldn't stop you from being Torah observant :)
(If someone can find a source for this story, I would love to hear about it.)