Both glitches and hazards seem to be very similar. Can the difference be explained with the help of an example?
In Digital electronics, a hazard is a glitch waiting to happen. For example, static and dynamic hazards in digital logic.
A hazard is a possibility, while a glitch is the event.
A glitch is an undesirable output, which might be for a short duration. For example, glitches are likely to be present on the non-registered outputs of Mealy Finite State Machines. Registering the outputs (i.e. adding a register at the output) reduces the likelihood of a glitch.
As an analogy, you might have heard of "choking hazard" - the hazard of choking. Choking is the event, and choking hazard is the possibility of choking.
Can glitch and hazard be used interchangeably?
No, glitch and hazard cannot be used interchangeably. For example, we say a glitch has occured, we never say a hazard has occured. We say a hazard exists, and this means that we know for sure that a glitch will occur under certain conditions, and we know what those conditions are.
"Glitch" is something of a moveable feast. Basically, it refers to a system behaving in an unexpected way.
At the lowest level, looking at a single signal, a glitch would be a pulse which appears where there shouldn't be one. Or, the signal changes polarity when it shouldn't.
At higher levels of abstraction, this can generally be described as a sudden, unexpected or inappropriate change in the variable being observed. A system which measures pressure in a vessel, for instance, which suddenly reports that the pressure has doubled, and then reverts to the original pressure, would be said to have experienced a glitch. Or, if a finite state machine controls a traffic light (and that was an early use of digital tech), and the light changes from green to red without an apparent yellow in between, that would be a glitch in the FSM operation, especially if it never happened again.
A hazard, in this sort of situation, is an identified condition which might produce a glitch. An example in the case of the traffic light FSM might be the realization that transitions in the internal states violate setup or hold times of the flip-flops. There are various types of hazards, and in complex systems some of them can be difficult to detect beforehand.
And Murphy's Law applies rigorously, one subsection specifying that some hazards will not manifest themselves during testing.