"Why is Y sometimes a vowel?"

Why so wishy-washy?

A, E, I, O, U, and sometimes Y. You might have learned it as a chant, a song, or a simple declaration, but this is how you learned the vowels of English. You may have wondered, why is Y so unsure of itself? Can't we just decide what it is? Why is Y a "sometimes" vowel?
Because writing is not the same thing as speech. While we casually refer to letters, which are written symbols, as vowels or consonants, the concepts of vowel and consonant properly belong to the domain of speech. In general terms, a consonant is a speech sound formed by some kind of constriction or impeding of air flow through the vocal tract, and a vowel lets the air flow freely through. The letter Y can stand for either of these types of sounds.
In "yes," Y is representing a consonant, and in "gym" it is representing a vowel.

Learn more here.