1. When I arrived at the station, the train had alrready left.

2. When I arrived at the station, he had been waiting for me for half an hour.

3. When I arrived at the station, he had waited for me for half an hour.

(Are they all grammatical? Which expressions should I use, #2 or #3? Thank you for your help.)

All are correct, but I'd use #2 rather than #3.

The progressive tense in #2 gives the sense of time passing until "I" arrived. In #3, it's not clear if he "had waited" (and then left) or if he "had waited" and was still waiting.

1 and 2 are acceptable; 3 is ambiguous.

All three sentences are grammatically correct, but there are slight differences in meaning between #2 and #3.

1. When I arrived at the station, the train had already left.
This sentence is in the past perfect tense. It means that when you arrived at the station, the train had already departed. The past perfect tense is used to indicate an action that was completed before another past action or point in time.

2. When I arrived at the station, he had been waiting for me for half an hour.
This sentence is also in the past perfect tense. It implies that someone had been waiting for you for half an hour before you arrived at the station. It emphasizes the duration of their waiting, indicating that they had started waiting before you arrived.

3. When I arrived at the station, he had waited for me for half an hour.
This sentence is in the past perfect tense as well. It suggests that someone had already waited for you for half an hour by the time you arrived. However, unlike sentence #2, it does not emphasize the duration of their waiting, but rather focuses on the completion of the waiting before you arrived.

To determine which expression is more appropriate, consider the context and what you want to convey. If you want to emphasize the duration of the waiting, use #2. If you want to focus on the completion of the waiting, use #3.