Actually, to be clear, vitamin B12 is a molecule containing the trace element cobalt. Herbivores and humans produce vitamin B12 in their guts via a bacterial process. The trouble for humans is that the B12 is produced in the large intestine where it cannot be absorbed.
As an aside, it’s important to note that some vitamin B12 supplements derive the cobalt-bearing molecule from animal sources while other supplements are synthetic. Taking a supplement does not always mean you aren’t using an animal product.
Also, Brewer’s and nutritional yeasts do not contain B12 unless they are fortified with it, and many are not.