Here’s a thought experiment in the legality of UK marriage:
A man and a pre-op transgender woman (man identifying as woman) outwork their relationship essentially as male and female, in a religious context. They are only extended the right of civil partnership, and denied marriage. They get civil partnered, but cannot do so in religious premises of their choice. After sexual reassignment surgery (SRS), the couple still outwork their relationship essentially as male and female. The couple legally remain civil partnered. When the legal sex of the transgendered individual is changed from male to female, the couple must legally separate, and can then enter into a marriage. Without the difficulty of declaring the SRS, the couple may now get married in religious premises of their choice.
Throughout the process, based on gender outworking, their relationship has not changed, but the legal/religious rules affect them differently throughout, likely causing added anxiety to their situation.
Yes, hard cases make bad laws, but good legislation avoids discrimination.