I think its the same reason school/slice of life stuff is popular, combined with magic, just made it a great combo.
I think school and school-aged life is heavily romanticized. Kids relate to it, and adults are nostalgic for it. It's a very comfortable and safe concept. A microcosm where you can love, learn, live, etc. Things are relatively safe. Your basic needs are met. You have friends, enemies, rivals. There's stressors but they're defined and not horrifying.There are basic rules and concepts are easy to understand (curfews, basic morals, etc) and this also gels well with rebellious attitude (breaking rules, doing the right thing).
Think where HP set it apart from something like say, Sabrina the Teenage Witch, is that it takes place in a mystical fun setting, versus a regular school. Gives just a extra dash of escapism that lets you seperate it from your own school experience, and imagine it as what you would want it to be.