Is transferring to another university an option for an unhappy PhD student? I've heard of people transferring between graduate schools because of their advisor moving to another school and taking their students with them. 

Do students ever transfer for other reasons? If a student isn't happy at the school they choose, could transferring be an option? Presumably they would frame it as something positive such as "turns out I really want to work with Professor X" rather than "I hate my school", but does this ever happen? Is there a certain timeframe in which this is possible? (Assume it's a 5 year PhD program.)

If the answer varies by subject please mention the subject in your answer.

(Context: many friends have told me that if it turns out I'm not happy with my choice, I can always transfer. My impression is that this is actually difficult since many schools don't accept transfer students and in any case not being able to stick with a program looks bad -- but I don't have evidence either way so wanted to ask people who are in academia. Of course the ideal would be to pick a school one is happy with in the first place!)
Mathematics

Let me specify that by transferring, I mean moving to another university in the middle of a graduate program.  Switching universities between undergrad and grad or between a master's degree and a Ph.D. is very different.  Basically, those are the only mainstream opportunities to move, and anything else will require an exception.

Transferring is certainly not unheard of, but there are some serious caveats:


It's difficult to transfer to a substantially more prestigious department.  It can happen, but the admissions committee will be very skeptical, and it's just not going to work out unless the application is outstanding.  In particular, many committee members specifically do not want to reward rolling the dice again and seeing what happens, so for transfers the question is not "Does this application look better than our weaker students?" but rather "Is this application so obviously wonderful that we would be shooting ourselves in the foot if we accepted someone else instead?"
Expressing any unhappiness will make it much harder to transfer.  You may think you'll be happy at the new school, but they will worry about ending up with an unhappy, unproductive grad student on their hands.  And they are right to worry about that: the best predictor of future happiness is past happiness, and many unhappy people have unrealistic beliefs about what would make them happy.
It's hard to transfer once you start seriously working on a thesis.  If your work isn't going well, then that's a reason not to accept you.  If your work is going well, then that's a reason you should continue working with your current advisor.  Unless your advisor has died or left, it will be tough to convince anyone you are a good candidate for transferring.
No matter why you say you want to transfer, there will be some suspicion that your goal is to end up in a stronger department or a more desirable location.  If you have another reason, you'll have to make a powerful argument for it.



  I've heard of people transferring between graduate schools because of their advisor moving to another school and taking their students with them. 


Sometimes they transfer officially, and sometimes they still get degrees from the previous school but complete their theses while in residence at the new school.  The latter is generally easy to arrange when an advisor moves.