Can a first author of a pre-print be demoted in the version sent to a journal? Can a PI decide to submit to a journal a previously agreed manuscript, uploaded to a pre-print server, and swap the first author with someone else after introducing some changes in the writing? Would the first author (student) have some way of defending himself against this?

EDIT: It is a field where author order matters.
Yes.

Preprints are preprints. They're preliminary versions. It's not surprising if the preprint differs from the version that's submitted to a journal. Author orders can even switch during revisions, during publication, etc.

You also ask about the first author "defending" himself/herself against this, which implies you think the order switch is unethical. In that case, speak to the other authors first, argue why the first author deserves to be the first author, and if they can't be convinced (+ you're equally convinced of your position), that's when you escalate.