What is the origin of situations in which you cannot expand a contraction?
Solution 1:
I would second the answer to this question that points out that constructions analogous to "why cannot I" were common through the 18th century and beyond, so though they sound old-fashioned today, they haven't always been ungrammatical.
But there's no real reason to insist that contemporary English and 18th-century English have to have identical grammar rules. In fact, Cambridge Grammar of the English Language (p. 91) argues that in contemporary English, can't and won't are independent words that are "negative inflections" of can and will, not true contractions. This argument is based largely on the observation that can't and won't can be used in places where cannot and will not are obsolete. (Negative inflections are uncommon in European languages but exist in other languages such as Japanese: arimasu = "to exist" and arimasen = "to be nonexistent").
Forms like won't are commonly regarded as 'contractions' of will + not, and so on, but there are compelling reasons for analysing them differently from cases like she'll (from she + will), they've (they + have), etc. Won't is, by every criterion, a single grammatical word, an inflectional form of will. She'll is not a single grammatical word, hence not an inflectional form. Rather, 'll (pronounced /l/) is a clitic form of will, i.e. a reduced form that is joined phonologically (and orthographically) to an independent word called its host. The host in the case of she'll is the pronoun she. The written forms she'll, they've, etc., are pronounced as single monosyllabic words phonologically but correspond to two-word sequences syntactically.
Evidence for this analysis is seen in:
[i] Won't/*Will not she be glad? [not replaceable by will not]
[ii] He says she'll read it, but she WON'T/will NOT.
Example [i] shows that won't is not always replaceable by will not (as she'll always is by she will), and in such cases a contraction analysis is not viable. In [ii] the [...] capitals indicate contrastive negation marked by stress. A clitic cannot bear stress (cliticisation is an extreme case of the phonological reduction that is available only for words that are unstressed). Note, for example, that in He says she won't read it, but she WILL, the stress prevents the reduction of she WILL to SHE'LL: if won't involved cliticisation like she'll, therefore, it would not occur with emphatic negation.
Solution 2:
Before we start, think of how "gonna" and "wanna" arose in English - they are phonetic corruptions of "going to" and "want to". They are relatively common even in writing in which the writer does not intend to mimic speech. The history of can't/cannot is much the same.
New Fowler's Modern English Usage (2000) states
The reduced form can't, which now seems so natural, is relatively recent in origin. It does not occur in the works of Shakespeare, for example, and the earliest example of it given in the OED is one of 1706
In "Negation in English and other Languages" by Jespersen, he comments
In writing the forms in n't make their appearance about 1660 and are already frequent in Dryden's, Congreve's, and Farquhar's comedies. Addison in the Spectator nr. 135 speaks of mayn't, can't, sha'n't, won't, and the like as having "very much untuned our language, and clogged it with consonants". Swift also (in the Tatler nr. 230) brands as examples of "the continual corruption of our English tongue" such forms as cou'dn't, ha'n't, can't, shan't; but nevertheless he uses some of them very often in his Journal to Stella.
You will see the parallel with "gonna" and "wanna"
Jespersen continues:
Cannot becomes can't with a different vowel, long [a:]; Otway 288 writes cannot, but pronounces it in one syllable [to add - can't]. Congreve 268 has can't. In the same way, with additional dropping of [l], shall not becomes [sha'nt] [to add - also with a long vowel]. The spelling was not, and is not yet, settled ; NED. records sha'nt from 1664, shan't from 1675, shann't from 1682 (besides Dryden's shan'not 1668); now both shan't and sha'n't are in use.
(Out of interest, Jespersen then goes on to refer to the lengthened "a" in the tag of "I'm angy, an't I?" in which the "m" is dropped and has given rise to the incorrect "I'm angry, aren't I?"... because that is what it sounds like...)
So, in short, cannot became can't because that is how the majority of people spoke and heard it. However, two spellings and two pronunciations are now accepted. The battle for "correctness" has ended in a compromise.
From the above, and in your example "Why cannot I do it?" I surmise that the "cannot" would have been popularly pronounced "can't" from somewhere in the early 1600s, yet spelled "cannot" and pronounced that way by teachers (who, by-and-large, are conservative.)
You will note that there are no circumstances in which can't may not replace "cannot", but "cannot" looks and sounds more "educated/formal" and may not replace "can't" in all circumstances. This is based on 400 years of pronunciation.