The fact that ドンピシャ (meaning "right on, fitting to a T") is written in katakana makes me wonder if it comes from a foreign phrase, but I can't figure out which one (out of the European languages I speak). Does anyone know?
どんぴしゃ is a native Japanese term, written in any script.
Or, if it's an originally Japanese word, can it be spelled with kanji, and if so, how?
It's an originally-Japanese word, but it doesn't have any kanji.
Some native vocabulary winds up in katakana because the original kanji is onerous to write. 蟻【あり】 ("ant") is one such example: anyone writing things out by hand will naturally gravitate to four-stroke アリ in preference for 18-stroke 蟻.
But some vocabulary winds up in katakana simply to make it stand out more from the rest of the text. Since Japanese doesn't use any whitespace, long strings of only-hiragana kind of all munge together and are visually harder to read. Changes in script help visually break things up, such as the contrast between hiragana and katakana, or between kana and kanji.
Adverbs are common targets of katakana-ization, since many of these are native terms that don't have kanji, and these might otherwise blend together with any particles or other hiragana-only spellings.
That all kind of mooshes together. Compare with hiragana + some katakana:
Slightly easier, I think. Using kanji makes it even easier:
Along similar lines, あれらはどんぴしゃとかさなった ⇒ あれはドンピシャと重なった. The katakana here doesn't indicate foreignness, and is instead used as a visual marker to improve parsing and legibility.
Where does ドンピシャ come from?
Let's expand upon the word's derivation.
This is a word from two parts: どん + ぴしゃ. I can't find a dated first citation for どんぴしゃ, but slightly longer form どんぴしゃり is first cited only relatively recently, to a book from 1964, with a similar sense of "exactly, just like so, perfectly fittingly".
This arose as an emphasized version of earlier ど, in turn from earlier どう, a Kansai-area innovation. This prefix was used as a pejorative to poke fun at the main noun. From what I can find, the earliest quote for ど is from 1705, and for どん from 1767. I suppose a distantly similar prefix in slang-y English might be derp, as in derpman or derpdog.
Over time, some uses of both ど and どん had more of the emphatic meaning and less of the pejorative meaning, with the prefix adding a sense more like "exactly, just so". Example terms include ど真【まん】ん中【なか】 ("exactly in the middle") or どんぴしゃ ("exactly fitting").
ぴしゃ appears in 1923, coming from earlier ぴしゃり cited in 1809. Both are adverbs, often found with the adverbial particle と. ぴしゃ also appears in the terms ぴしゃん and ぴしゃぴしゃ, also adverbs. This seems to be a later sound shift from synonymous older root びしゃ, with adverb びしゃり dating to at least 1603.
All of these ぴしゃ terms share similar senses, mainly relating to the sound symbolism of the core portion ぴしゃ -- it's basically a sound effect that became a word unto itself. The main meanings have to do with "water splashing" or "something sliding shut suddenly after being pushed, like a door or window".
The idea of "shutting quickly" then extended to mean "fitting perfectly", the way a door or window usually fits perfectly into its frame.
Thus, we have どん (emphatic: "exactly so") + ぴしゃり ("exactly fittingly"), then shortening to どんぴしゃ. Alternatively, we have どん + ぴしゃ directly combining, but given the earlier date for ぴしゃり, I suspect the version with り came first.