How do I merge along a single axis?
There are two ways to go about doing this.

Method one is to merge the front edges first, then the back ones. If the vertices you are merging have no depth on a certain axis (the Y axis in your case) then merging them will not move them on that axis. Simply select the from ones, merge them, then the back, and merge them.

The second Method is to select all the vertices that you want to merge and scale them to 0 on whatever axis you want to merge the vertices along. In this case, you would press S then X and finally 0 to completely flatten the vertices along the X axis. If the vertices you have selected had some variations along the Z axis, you could instead use Shift+Y instead of X to scale on all axes but the Y, so that they would be perfectly flat. Confirm the scale operation, and then, with all of the vertices still selected, press W and select remove doubles. This will merge together any vertices that are in the exact same position.

One other thing I should mention is that blender has a feature called Auto merge editing that will automatically merge vertices that are in the exact same place. This could be used to save a step in method two. The feature can be found at the bottom of the 3D view on the right side of the toolbar there. It is the little button with the two arrows pointing to the center.

Method two sounds longer when I describe it here, but in practce, once you become familiar with the shortcuts it is quicker that way.