The coincidence locus of two scheme morphisms is the fibre product
The diagonal morphism is a locally closed immersion, and this property is stable under base change of a morphism of schemes, so is locally closed. The universal property of a fibre product says that a morphism factors through exactly when . Thus is the largest locally closed subscheme on which they coincide. If is separated, is a closed immersion, and its base change is closed.
Fix and choose affine opens and with . The open subset of contains . If and , its diagonal is induced by the surjection
so it is a closed immersion. Hence every diagonal morphism is locally a closed immersion into an open subset, and therefore is a locally closed immersion.
For , the product is and the diagonal is . Its complement is the principal affine open
Take instead the separated scheme . The complement of its diagonal in is , where the removed diagonal has codimension two. Its global functions still form the polynomial ring of . Were the complement affine, its canonical morphism to of this ring would identify it with all of , which is impossible. Thus this diagonal complement is not affine.