I consider the speaking is correct, as 同学 is composed of two words "同" and "学", put the words together, it becomes a noun - "schoolmate", but the words can be used separately and interpreted as "learning/study(学) together/with(同)", and implies "a shared(同) experience or place(学校)".
我和他同过三年学。- "He and I were classmates for three years." implies the shared experience in the same place - He and I have/had been learning/studying together/in the same school for three years.
A similar expression is "我和他同过三年房/宿舍" - He and I (we) have shared a room (under the same roof) for three years. The equivalent is "我們做過三年的室友". And "我和她同过三年床" - "We have slept together for three years", or, depending on the context of the question that draws the answer, "We were spouses for three years".