The reason it's spelt this way is because "why" is derived from the Old English word "hwī," which itself came from the Proto-Germanic word "hwē."
The "hw" sound in Old English evolved into the modern English "wh" sound, and the "ī" sound in Old English became the modern English "y" sound.
So, the spelling "why" reflects the historical evolution of the word from its Old English roots.