It's a matte shot (Wikipedia) according to SlantMagazine.
Early examples of Hitchcock’s trademarks abound in both films: mistaken identities, match editing, imaginative tight-quarters shooting—including in Lady Vanishes a terrific matte shot of Gilbert hanging out of a train window while another train approaches and blows by.
Matte shots have been around since the 1900's...more at the wikipedia link.
Mattes are used in photography and special effects filmmaking to combine two or more image elements into a single, final image. Usually, mattes are used to combine a foreground image (such as actors on a set, or a spaceship) with a background image (a scenic vista, a field of stars and planets). In this case, the matte is the background painting. In film and stage, mattes can be physically huge sections of painted canvas, portraying large scenic expanses of landscapes.
In film, the principle of a matte requires masking certain areas of the film emulsion to selectively control which areas are exposed. However, many complex special-effects scenes have included dozens of discrete image elements, requiring very complex use of mattes, and layering mattes on top of one another. For an example of a simple matte, we may wish to depict a group of actors in front of a store, with a massive city and sky visible above the store's roof.