Can UK PhD studentships legally effectively bar EU applicants by stating that "all eligible applicants must also have been resident in the UK"?
You have answered your own question in the question: states can (under EU law) put conditions on, e.g., funding, based on residence, but not based on citizenship. As UK citizens who are not ordinarily resident would not be eligible for this funding, this does not discriminate on citizenship, so this is legal (*).

Anecdotally, in my experience a low single-digit percentage of "UK" applicants fail the ordinarily resident test, and I have seen at my institution roughly 5 students over the past 10 years refused funding from similar sources for this reason.

(*) I am definitely not a lawyer: the case law around ordinarily resident status shows that this issue is not unique to universities.