A federal judge has said the University of North Carolina must allow two transgender students and one employee to use the restroom of their choice while the legal fight over the state’s bathroom law plays out in court.

U.S. District Judge Thomas Schroeder dealt a blow to Republican Gov. Pat McCrory and legislative leaders by granting a partial preliminary injunction against the law known as House Bill 2.

The...